/**
 * Scan Activity
 * @author ZXingQuickStart/brought by Hadi Rashid
 */


package com.cats.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

import com.cats.R;
import com.cats.camera.BoundingView;
import com.cats.camera.CameraManager;
import com.cats.camera.CameraPreviewView;
import com.cats.camera.CaptureHandler;
import com.cats.camera.PreviewCallback;

/**
 * Capture activity (camera barcode activity)
 */
public class ScanActivity extends BaseActivity {
    /**
     * Camera preview view
     */
    private CameraPreviewView cameraPreview;
    /**
     * Camera manager
     */
    private CameraManager cameraManager;
    /**
     * Capture handler
     */
    private Handler captureHandler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);
        setActionBarTitle("Scan");

        // Create an instance of Camera
        cameraManager = new CameraManager();
        captureHandler = new CaptureHandler(cameraManager, this, new OnDecoded());
        //requesting next frame for decoding
        cameraManager.requestNextFrame(new PreviewCallback(captureHandler, cameraManager));

        // Create our Preview view and set it as the content of our activity.
        cameraPreview = (CameraPreviewView) findViewById(R.id.camera_preview);
        cameraPreview.setCameraManager(cameraManager);
        ((BoundingView) findViewById(R.id.bounding_view)).setCameraManager(cameraManager);
    }

    @Override
    protected void onPause() {
        super.onPause();
        cameraManager.release();
    }

    private class OnDecoded implements CaptureHandler.OnDecodedCallback {
        @Override
        public void onDecoded(String decodedData) {
//            Toast.makeText(ScanActivity.this, decodedData, Toast.LENGTH_SHORT);
            
            /**
             *  Junshuang added: return result, finish activity
             */
            Intent intent = new Intent();
			Bundle b = new Bundle();
			b.putString("BARCODE", decodedData);
			intent.putExtras(b);
			setResult(RESULT_OK, intent);
			finish();            
        }
    }
    
    
}
